﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Solidi
{
	public class DependencyGraph
	{
		private IDependency _dependency;
		private DependencyChildLifetime _lifetime;
		private IDependencyReference _reference;

		public DependencyGraph(IDependencyContext context, IDependency dependency)
        {
            _dependency = dependency;

			_lifetime = new DependencyChildLifetime(context);

			_reference = dependency.Resolve(_lifetime);
        }

		public object Value
		{
			get
			{
				if (_reference != null)
				{
					return _reference.Value;
				}

				return null;
			}
		}

		public DependencyDisposal Release()
		{
			DependencyDisposal disposal = _dependency.Release(_reference);

			if (disposal == DependencyDisposal.Disposed)
			{
				_lifetime.ReleaseChildren();
			}

			return disposal;
		}
	}
}
